package com.meepo.framework.design.desc

import android.content.Context
import android.text.TextUtils
import android.util.AttributeSet
import android.util.TypedValue
import android.view.View
import android.widget.LinearLayout
import android.widget.RelativeLayout
import com.meepo.framework.R
import com.meepo.framework.design.helper.Utils
import com.meepo.framework.design.helper.ImageLoader
import kotlinx.android.synthetic.main.desc_layout.view.*

class DescLayout @JvmOverloads constructor(private val mContext: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0, defStyleRes: Int = 0)
    : LinearLayout(mContext, attrs, defStyleAttr, defStyleRes) {

    private var mIconSize: Int
    private var mTitleSize: Float
    private var mTitleColor: Int
    private var mDescSize: Float
    private var mDescColor: Int

    private lateinit var mData: Data

    fun setData(data: Data) {
        mData = data
        notifyDataChanged()
    }

    private fun notifyDataChanged() {

        mData.let {

            avatar_ci.layoutParams = RelativeLayout.LayoutParams(mIconSize, mIconSize)
            if (!TextUtils.isEmpty(it.avatar)) {
                ImageLoader.loadImage(mContext, it.avatar, avatar_ci)
            } else {
                avatar_ci.visibility = View.GONE
            }


            title_tv.setTextSize(TypedValue.COMPLEX_UNIT_PX, mTitleSize)
            title_tv.setTextColor(mTitleColor)
            if (!TextUtils.isEmpty(it.title)) {
                title_tv.text = it.title
            } else {
                title_tv.visibility = View.GONE
            }


            if (!TextUtils.isEmpty(it.tag)) {
                tag_view.text = it.tag
            } else {
                tag_view.visibility = View.GONE
            }


            desc_tv.setTextSize(TypedValue.COMPLEX_UNIT_PX, mDescSize)
            desc_tv.setTextColor(mDescColor)
            if (!TextUtils.isEmpty(it.desc)) {
                desc_tv.text = it.desc
            } else {
                desc_tv.visibility = View.GONE
            }
        }

    }

    init {
        val view = View.inflate(mContext, R.layout.desc_layout, null)
        val ta = context.obtainStyledAttributes(attrs, R.styleable.DescLayout)

        mIconSize = ta.getDimensionPixelSize(R.styleable.DescLayout_iconSize, Utils.dp2px(mContext, 37f))
        mTitleSize = ta.getDimension(R.styleable.DescLayout_titleSize, Utils.sp2px(mContext, 14f).toFloat())
        mTitleColor = ta.getColor(R.styleable.DescLayout_titleTextColor, mContext.resources.getColor(R.color.primary_text_color))
        mDescSize = ta.getDimension(R.styleable.DescLayout_descSize, Utils.sp2px(mContext, 12f).toFloat())
        mDescColor = ta.getColor(R.styleable.DescLayout_descTextColor, mContext.resources.getColor(R.color.sub_text_color))

        ta.recycle()

        this.addView(view)
    }
}